﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：ITypedCache.cs
// 功能描述：TongTongMall
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TongTongMall.Runtime.Caching
{
    /// <summary>
    /// 类型化接口缓存
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    public interface ITypedCache<TKey, TValue> : IDisposable
    {
        /// <summary>
        /// 缓存过期时间.
        /// </summary>
        TimeSpan DefaultSlidingExpireTime { get; set; }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        TValue Get(TKey key, Func<TKey, TValue> factory);

        /// <summary>
        /// 异步获取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        Task<TValue> GetAsync(TKey key, Func<TKey, Task<TValue>> factory);

        /// <summary>
        /// 获取默认缓存或返回null
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        TValue GetOrDefault(TKey key);

        /// <summary>
        /// 获取默认缓存或返回null
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<TValue> GetOrDefaultAsync(TKey key);

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="slidingExpireTime"></param>
        void Set(TKey key, TValue value, TimeSpan? slidingExpireTime = null);

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="slidingExpireTime"></param>
        Task SetAsync(TKey key, TValue value, TimeSpan? slidingExpireTime = null);

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key"></param>
        void Remove(TKey key);

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key"></param>
        Task RemoveAsync(TKey key);

        /// <summary>
        /// 清空类型缓存
        /// </summary>
        void Clear();

        /// <summary>
        /// 清空类型缓存
        /// </summary>
        Task ClearAsync();
    }
}
